import store from '@/store/store'
import router from '@/router/index'
import https from '@/public/https'
/**
 * ======================================================================
 * 【基本说明】：
 * MobileIMSDK-Web版前端接收服务端回调。
 *
 * Created by Major_ on 18-7-5.
 * ======================================================================
 */

 // 根据消息指纹，拉取消息
 function  postObtainMessageContent(data,callback){
        let group_id;
        if(data.typeu)
            group_id = data.typeu;
        let options = {
            fingerprint: data.fp,
            group_id: group_id
        }

        https.post("Chat/postObtainMessageContent",options)
        .then(data => {
            if(data.status){
                callback(data.data);
            }else{
                console.log(data.message);
            }
        })
    }


export default{
    log (message, toConsole){
        console.log('log...',message,toConsole);
    },

    showChatMessage(data,options){
        // 如果p.from等于打开窗口id;则在消息窗口中加一条消息，否则在该用户后面加有个未读标识
        postObtainMessageContent(data, function(res){
            let p = data;
            let id = p.from;
            if(!res.content)
                res.content = options;
            if(p.from == store.state.toUserInfo.id || p.from == store.state.userInfo.id || p.typeu == store.state.toUserInfo.group_id){
                if(p.typeu == store.state.toUserInfo.group_id){
                    // 如果打开为群聊天窗口
                    var msg = {
                        nickname:'',
                        group_id:'',
                        send_uid:p.from,
                        content: res.content,
                        create_time: Math.round(new Date().getTime()/1000)
                    }
                    store.commit('setMsgList',msg);
                    if(p.typeu == store.state.toUserInfo.group_id){
                        https.post('chat/postUpdateMessageStatus',{uid:IMSDK.getLoginInfo().loginUserId,group_id: store.state.toUserInfo.group_id,from_uid:store.state.toUserInfo.id})
                        .then(data => {
                            if(data.status == 0){
                                console.log('错误信息：', data.message);
                            }
                        });
                    }
                }else if(p.typeu == -1 || p.typeu == 0){
                    // 如果打开为好友聊天窗口
                    var msg = {
                        nickname:'',
                        group_id:'',
                        from_uid:p.from,
                        content: res.content,
                        create_time: Math.round(new Date().getTime()/1000)
                    }
                    store.commit('setMsgList',msg);
                    if(p.from == store.state.toUserInfo.id){
                        https.post('chat/postUpdateMessageStatus',{uid:IMSDK.getLoginInfo().loginUserId,group_id: store.state.toUserInfo.group_id,from_uid:store.state.toUserInfo.id})
                        .then(data => {
                            if(data.status == 0){
                                console.log('错误信息：', data.message);
                            }
                        });
                    }
                }else{
                    let msgUserList = store.state.msgUserList;
                    for(let i in  msgUserList){ //将消息列表未读+1;
                        if(msgUserList[i].group_id == p.typeu){
                            msgUserList[i].un_read_num ++; 
                        }   
                }
                store.state.userInfo.un_read_num ++;
                }
            }else{
                let msgUserList = store.state.msgUserList; //消息用户列表
                let UserList = store.state.userList; //用户列表
                if(p.typeu == -1 || p.typeu == 0){
                    for(let i in  msgUserList){ //将消息列表未读+1;
                        if(msgUserList[i].id == id){
                            msgUserList[i].un_read_num ++; 
                        }   
                }
                for(let i in UserList){ //将用户列表中未读+1;
                    for(let k in UserList[i].data){
                        if( UserList[i].data[k].id == id){
                            UserList[i].data[k].un_read_num ++;
                            UserList[i].un_read_total_num ++;
                        }
                    }
                }
                store.state.userInfo.un_read_num ++;
                }else{
                    for(let i in  msgUserList){ //将消息列表未读+1;
                        if(msgUserList[i].group_id == p.typeu){
                        msgUserList[i].un_read_num ++; 
                        }   
                }
                store.state.userInfo.un_read_num ++;
                }
            }
        });
      },
    afterLoginSucess(){ //登录成功！！
        let uid = store.state.userInfo.id;
        let is_expire =  store.state.userInfo.is_expire;
        if(is_expire == 1){
            router.push('/MembershipRecharge');
        }else{
            router.push('/layout');
        }
    },
    afterLoginSucessFailed(isReconnect){ //登录或重连回调
        console.log('对不起，你'+(isReconnect?'自动重连':'登陆')+'IM服务器失败了 ...', false);
      },
    afterDisconnected(){ //与IM服务端的网络连接断开时要调用的函数。
        console.log('Sorry，你掉线了 ...', false);
      },
    afterReconnectSucess(){ //掉线重连成功时要调用的函数。
        console.log('掉线自动重连成功了！', false);
    }
}